package com.gzsxy.esjy.extraldecoder;

import com.gzsxy.esjy.handler.BaseHandler;

import com.gzsxy.esjy.request.HttpServletRequest;
import io.netty.channel.Channel;
import io.netty.handler.codec.http.HttpHeaderNames;
import io.netty.handler.codec.http.HttpHeaderValues;
import io.netty.handler.codec.http.HttpRequest;

public interface IBusinessDecoder<T> {
	
	public BaseHandler<T> ready(Channel ctx, HttpRequest request) ;
	
	
	public void decode() ;
	
	/**
	 * 判断是否长连接
	 * 
	 * @param request
	 * @return
	 */
	default boolean isKeepAlive(HttpServletRequest request) {
		String connection = request.getHeaders().get(HttpHeaderNames.CONNECTION);
		if (connection != null && HttpHeaderValues.CLOSE.contentEqualsIgnoreCase(connection)) {
			return false;
		}

		if (request.getProtocolVersion().isKeepAliveDefault()) {
			return !HttpHeaderValues.CLOSE.contentEqualsIgnoreCase(connection);
		} else {
			return HttpHeaderValues.KEEP_ALIVE.contentEqualsIgnoreCase(connection);
		}
	}
	
	/**
	 * 判断是否长连接
	 * 
	 * @param request
	 * @return
	 */
	default boolean isKeepAlive(HttpRequest request) {
		String connection = request.headers().get(HttpHeaderNames.CONNECTION);
		if (connection != null && HttpHeaderValues.CLOSE.contentEqualsIgnoreCase(connection)) {
			return false;
		}

		if (request.protocolVersion().isKeepAliveDefault()) {
			return !HttpHeaderValues.CLOSE.contentEqualsIgnoreCase(connection);
		} else {
			return HttpHeaderValues.KEEP_ALIVE.contentEqualsIgnoreCase(connection);
		}
	}
	
}
